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x09 ,0x03 ,0x99 ,0x02 ,0x12 ,0x03 ,0xab ,0x02 ,0x29 ,0x03 ,0xb3 ,0x02 ,0x40 ,0x03 ,0xab ,0x02]]
class position:
    def __init__(self):
        self.obj = []
        self.elements = []
        self.elem_x  = []
        self.elem_y  = []
        
class ts:
    def __init__(self):
        self.xp=0
        self.yp=0
        self.xps=[]
        self.yps=[]
        self.xpoints=[]
        self.ypoints=[]
        self.points=[]
        self.layout = position()
    def getPos(self,file_name):
        fd = open(file_name)
        _pos = fd.readlines()
        pos = []
        for p in _pos:
            p = p[:-2].split()
            pp=[]
            for n in p:
                pp.append(int(n,16))
            pos.append(pp)
        fd.close()
        #print pos
        return pos
    def draw_layout(self,pos):
        for i in range(0,len(pos)):
            posi=pos[i]
            length = len(posi)
            j=0
            while(j<length):
                self.xp = (posi[j+1]*256) + posi[j]
                self.yp = (posi[j+3]*256) + posi[j+2]
                #print"(%d %d) " % (self.xp,self.yp ) , 
                self.layout.elem_x.append(self.xp)
                self.layout.elem_y.append(self.yp)
                j += 4
            self.layout.elements.append(self.layout.elem_x)
            self.layout.elements.append(self.layout.elem_y)
            self.layout.obj.append(self.layout.elements)
            self.layout.elements=[]
            self.layout.elem_x=[]
            self.layout.elem_y=[]
            print''
        #self.points.append(self.xpoints)
        #self.points.append(self.ypoints)
        self.__draw()
    def __draw(self):
        plt.plot(0,0)
        plt.plot(800,600)
        for elem in self.layout.obj:#get one of keys
            plt.plot(elem[0],elem[1],'r-')
        plt.xlabel("x value")
        plt.ylabel("y value")
    def draw_point(self,x,y):
        plt.plot(x,y,'b.')
    def show(self):
        plt.show()

class dataparse:
    def __init__(self,data_flow):
        self.head='02'
        self.length=''
        self.cmd=''
        self.data=''
        self.eof='03'
        self.lrc='0000'
        self.data_flow=data_flow
        self.count=0
        self.length = len(data_flow)
        print"length:",self.length
    def get_char(self):
        ret = '-1'    
        if(self.count<self.length):
            ret = self.data_flow[self.count].upper()
            self.count += 1            
        else:
            ret = '-1'
        return ret
    def get_cmd(self,cmd_code):
        cmds = parse.get_all_cmds()
        ret_cmds=[]
        for i in range(0,len(cmds)):
            #print cmds[i]
            if cmds[i][4] == cmd_code.upper():
                ret_cmds.append(cmds[i])
        return ret_cmds
    def get_all_cmds(self):
        cmds=[]
        self.count = 0
        while 1:
            head=''
            ilength=0
            slength=''
            data=''
            cmd=''
            eof=''
            lrc=''
            head = self.get_char()
           
            if(head == '-1'):
                break
            else:                
                if head == '02':
                    tmp = ''
                    ilength=0
                    #print" ",head,
                    tmp = self.get_char()
                    ilength = 100*int(tmp.decode("HEX"))
                    slength += tmp
                    #print" ",tmp,
                    tmp = self.get_char()
                    ilength += 10*int(tmp.decode("HEX"))
                    slength += tmp
                    #print" ",tmp,
                    tmp = self.get_char()
                    ilength += int(tmp.decode("HEX"))
                    slength += tmp
                    #print" ",tmp,
                    cmd = self.get_char()
                    #print"len:",ilength
                    while ilength-1:
                        data += self.get_char()
                        ilength -= 1
                    #print" ",data,
                    eof = self.get_char()
                    if eof != '03':
                        print "package error"
                        return
                    else:
                        lrc  = self.get_char()
                        lrc += self.get_char()
                    cmd = head + slength + cmd + data + eof + lrc 
                    cmd = re.findall(r'(.{2})',cmd)
                    cmds.append(cmd)
        return cmds
def get_list(cmds):
    """
    参数为[][]类型
    """
    j=0
    i=0
    if(not len(cmds)):
        print "list empty"
        return None
    lists =[]
    lst=[]
    for icmd in cmds:
        j +=1
        if j%2:
           continue
        i=0
        length = len(icmd[9:-3])
        #print icmd[9:-3]
        for x in icmd[9:-3]:
            lst.append(int(x,16))       
        lists.append(lst)
        lst=[]
    return lists
def get_raw_array(cmds):
    """
    参数为[][]类型
    """
    j=0
    i=0
    if(not len(cmds)):
        print "list empty"
        return None
    array = '[%d][%d]=:\n' % (len(cmds)/2,len(cmds[0]))
    for icmd in cmds:
        j +=1
#        if j%2:
#           continue
        i=0
        #cmd = icmd[5:-3]
        cmd = icmd
        length = len(cmd)
        #print cmd
        for x in cmd:
            i += 1            
            array += "%s "%x
            if i==(length):
                array += '\n'
    array += '\n'
    return array
def get_c_array(cmds):
    """
    参数为[][]类型
    """
    j=0
    i=0
    if(not len(cmds)):
        print "list empty"
        return None
    array = 'unsigned char cmds[%d][%d]={\n' % (len(cmds)/2,len(cmds[0]))
    for icmd in cmds:
        j +=1
        if j%2:
           continue
        i=0
        #cmd = icmd[5:-3]
        cmd = icmd
        length = len(cmd)
        #print cmd
        for x in cmd:
            if i==0:
                array += '{'
            i += 1            
            array += "0x%s,"%x
            if i==(length):
                array += '},\n'
    array += '};\n'
    return array
def filefilter(file):
    valid_data_idx=0
    lst=[]
    fd = open(file)
    dat = fd.readlines()
    dat_len = len(dat)
    #过滤非数据头部
    for idx in range(0,dat_len):
        if(dat[idx].startswith('------')):
            valid_data_idx = idx
            break
    valid_data_idx += 1
    #获得数据
    for i in range(valid_data_idx,dat_len):
        lst += dat[i][25:76].split()
    fd.close()        
    return lst
if __name__ ==  '__main__':
    ts=ts()
    position = ts.getPos('./logLayout.txt')
    #ts.draw_layout(position)
    #for i in range(100):
    #    ts.draw_point(rand.random()*1000,rand.random()*1000)
    #ts.show()

    lst = filefilter('./2.txt')
    #print lst
    parse = dataparse(lst)
    cmds = parse.get_all_cmds()
    cmds = parse.get_cmd('fe')
    print get_raw_array(cmds)
    #for cmd in cmds:
    #    print cmd
    cmds = parse.get_cmd('7d')
    #print get_c_array(cmds)
    #lst=get_list(cmds)
    #for L in lst:
    #   i=0
    #   for x in L:
    #       #print x,
    #       if not i%8:
    #           #print ''
    #           pass
    #       i+=1
    #ts.draw_layout(get_list(cmds))
    #ts.show()